import QtQuick
import QtQuick.Controls
import Qt.labs.platform

ApplicationWindow {
    id: root
    width: 840
    height: 520
    minimumWidth: 840
    minimumHeight: 480
    visible: true
    color: "transparent"
    flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint | Qt.WindowMinMaxButtonsHint

    property int showSubWinNum: 0

    function openSubWin() {
        if(showSubWinNum > 0) {
            tipsTextRect.showTips(qsTr("请先关闭打开的子窗口"))
            return false
        } else {
            showSubWinNum += 1
            return true
        }
    }

    Connections {
        target: user

        function onConfigProgressChanged(current, target) {
            configProgress.cpbValue = current / target
        }
    }

    // 托盘 ApplicationWindow可用
    SystemTrayIcon {
        visible: true
        icon.source: "qrc:/imgs/canFilterTool.png"

        onActivated: {
        }

        menu: Menu {
            id: menu
            MenuItem {
                text: qsTr("显示主界面")
                onTriggered: root.show()
            }
            MenuSeparator {}
            MenuItem {
                text: qsTr("退出")
                onTriggered: Qt.quit()
            }
         }
    }

    // 遮罩层
    Rectangle {
        id: shadeRect
        width: parent.width
        height: parent.height
        color: "#e0e1e2"
        visible: false
        opacity: 0.4
        z: 10

        onVisibleChanged: {
            progressRect.visible = visible
        }

        // 屏蔽部分鼠标操作
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            propagateComposedEvents: false
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onWheel: {}
            onEntered: {}
            onExited: {}
        }
    }
    // 进度条
    Rectangle {
        id: progressRect
        anchors.centerIn: parent
        width: 300
        height: 200
        visible: false
        color: "#2e2f30"
        z: 11
        radius: 8

        Column {
            x: 8
            width: parent.width - x * 2
            height: parent.height
            spacing: 56

            AsText {
                id: progressTitle
                x: (configProgress.width - width) / 2
                height: 28
                text: qsTr("操作进行中...")
                font.pixelSize: 14
                color: "#e1e4e8"
                verticalAlignment: Text.AlignVCenter
            }

            CustomProgressBar1 {
                id: configProgress
                width: parent.width
                height: 20
                cpbForeGradient: 100
                cpbBackColor: "#44484d"
                cpbTextColor: "#FFFFFF"

                onAnimationOver: {
                    exitProgressBtn.visible = true
                }
            }

            AsButton {
                id: exitProgressBtn
                x: (parent.width - width) / 2
                width: 60
                height: 30
                text: qsTr("确认")
                itemRadius: 8
                visible: false

                onClicked: {
                    shadeRect.visible = false
                    visible = false
                }
            }
        }
    }

    // 关于软件页面
    Loader {
        id: aboutSoftwareWinLoader

        onLoaded: {
            aboutSoftwareWinSignal.target = aboutSoftwareWinLoader.item
        }

        Connections {
            id: aboutSoftwareWinSignal

            function onClosing() {
                aboutSoftwareWinLoader.source = ""
                showSubWinNum = 0
            }
        }
    }
    // 使用说明页面
    Loader {
        id: useSoftwareWinLoader

        onLoaded: {
            useSoftwareWinSignal.target = useSoftwareWinLoader.item
        }

        Connections {
            id: useSoftwareWinSignal

            function onClosing() {
                useSoftwareWinLoader.source = ""
                showSubWinNum = 0
            }
        }
    }
    // 提示页面
    TipsTextWin {
        id: tipsTextRect
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        width: 160
        height: 50
        visible: false
        radius: 10
        z: 100

        onFocusChanged: {
            visible = false
        }
    }

    ResizeItem {
        id: resizeItem
        anchors.fill: parent
        enableSize: 8
        focus: true

        // 头部工具栏
        CustomHeaderBar {
            id: customHeaderBar
            anchors.top: parent.top
            width: parent.width
            height: 34
            color: "#1f2428"
            leftSideWidth: 100
            rightSideWidth: 150
            titleText: qsTr("CAN Filter Tool V1.0")
            z: 9

            onSpinnerBtnFileClicked: function(index) {
                switch(index) {
                    case 0: Qt.quit(); break;
                    default: focus = true; break;
                }
            }

            onSpinnerBtnHelpClicked: function(index) {
                switch(index) {
                    case 0: // 使用说明
                        focus = true
                        if(openSubWin())
                            useSoftwareWinLoader.source = "UseSoftwareWin.qml"
                        break;
                    case 1: // 关于软件
                        focus = true
                        if(openSubWin())
                            aboutSoftwareWinLoader.source = "AboutSoftwareWin.qml"
                        break;
                    default: focus = true; break;
                }
            }
        }

        LeftSideMenuBar {
            id: leftSideMenuBar
            anchors.left: parent.left
            anchors.top: customHeaderBar.bottom
            anchors.bottom: parent.bottom
            menuBarWidth: 50
            menuItemWidth: 150
            z: 8

            onActionErrorTips1: function(tips) {
                tipsTextRect.showTips(tips)
            }
        }

        Item {
            anchors.left: leftSideMenuBar.right
            anchors.right: parent.right
            anchors.top: customHeaderBar.bottom
            anchors.bottom: parent.bottom

            Row {
                anchors.fill: parent

                FilterIDEditView {
                    id: filterIDEditView1
                    width: parent.width / 2
                    height: parent.height
                    color: "#373839"
                    editTitle: qsTr("CAN1过滤ID配置:")
                    canPort: 1

                    onShowProgress: {
                        if(user.getMyProperty("serialState")) {
                            configProgress.reset()
                            shadeRect.visible = true
                        } else {
                            tipsTextRect.showTips(qsTr("请先连接设备"))
                        }
                    }
                }

                FilterIDEditView {
                    id: filterIDEditView2
                    width: parent.width / 2
                    height: parent.height
                    color: "#2e2f30"
                    editTitle: qsTr("CAN2过滤ID配置:")
                    canPort: 2

                    onShowProgress: {
                        if(user.getMyProperty("serialState")) {
                            configProgress.reset()
                            shadeRect.visible = true
                        } else {
                            tipsTextRect.showTips(qsTr("请先连接设备"))
                        }
                    }
                }
            }
        }
    }
}
